package com.lori.netshop.service.imp;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.lori.netshop.pojo.TbUser;
import com.lori.netshop.service.UserService;
import com.lori.netshop.util.CookieUtils;
import com.lori.netshop.util.HttpClientUtil;
import com.lori.netshop.util.NetshopResult;
@Service
public class UserServiceimp implements UserService {
	@Value("${BASE_SSO}")
	private String BASE_SSO;
	@Value("${SSO_TOKEN}")
	private String SSO_TOKEN;
	@Value("${SSO_LOGINOUT}")
	private String SSO_LOGINOUT;
	/**
	 * 查询用户
	 */
	@Override
	public NetshopResult findByToken(String token) {
		//使用httpclient调用单点登录系统的用户
		String doGet = HttpClientUtil.doGet(BASE_SSO+SSO_TOKEN+token);
		//转为netshopresult
		NetshopResult result = NetshopResult.formatToPojo(doGet, TbUser.class);
		return result;
	}

	/**
	 * 安全退出
	 */
	@Override
	public NetshopResult loginOut(HttpServletRequest request) {
		//获得cookie里的token
		String cookieValue = CookieUtils.getCookieValue(request, "token");
		//使用httpclient调用单点登录系统用户退出功能
		String doGet = HttpClientUtil.doGet(BASE_SSO + SSO_LOGINOUT + cookieValue);
		//转换数据
		NetshopResult result = NetshopResult.format(doGet);
		return result;
	}

}
